package com.example.xueshen.common.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xueshen.common.entity.Ban;
import com.example.xueshen.common.entity.PageData;
import com.example.xueshen.common.entity.Students;
import com.example.xueshen.common.service.BanService;
import com.example.xueshen.common.service.StudentsService;
import com.example.xueshen.common.util.MyPageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
public class StudentsController {
    @Resource
    private StudentsService studentsService;
    @Resource
    private BanService banService;

    @GetMapping("/toadd")
    public String toAdd(Map<String, Object> map) {
//        List<Students> students = studentsService.selectAllByPlus();
        List<Ban> bans = banService.selectAllByPlus();
        map.put("bans", bans);
//        map.put("students", students);
        return "student/abc :: addOrUpdate";
    }

    @GetMapping("/query")
    public String select(PageData pageData, Map<String, Object> map, String studentName) {
        Page<Students> page = new Page<>(pageData.getCurrent(), pageData.getSize());
        if (studentName != null) {
            //模糊查询
            Page<Students> studentsPage = studentsService.selectByLike(studentName, page);

//        分页
//        Page<Students> studentsPage = page.setRecords(students);

            for (Students student : studentsPage.getRecords()) {
                Ban ban = banService.selectOneById(student.getClassId());
                student.setLass(ban);

            }
            if (page.getTotal() != 0) {
                int[] pageNums = MyPageUtil.getNavigatePageNums((int) studentsPage.getCurrent(), (int) studentsPage.getPages(), 3);
                map.put("pageNums", pageNums);
            }
            map.put("studentName", studentName);
            map.put("page", studentsPage);

        }else {

            Page<Students> studentsPage = studentsService.selectAllByPlusAndPage(page);

            for (Students student : studentsPage.getRecords()) {
                Ban ban = banService.selectOneById(student.getClassId());
                student.setLass(ban);

            }
            if (page.getTotal() != 0) {
                int[] pageNums = MyPageUtil.getNavigatePageNums((int) studentsPage.getCurrent(), (int) studentsPage.getPages(), 3);
                map.put("pageNums", pageNums);
            }

            map.put("page", studentsPage);
        }

        return "student/abc";

    }

    @PostMapping("/student")
    @ResponseBody
    public String add(@Valid Students students) {
        if (students.getStudentId() != null) {
            Boolean flag = studentsService.updateById(students);
            return flag ? "修改成功" : "修改失败";
        }
        Boolean flag = studentsService.insert(students);
        return flag ? "添加成功" : "添加失败";
    }


    @GetMapping("/student/{studentId}")
    public String selectById(@PathVariable("studentId") Integer id, Map<String, Object> map) {
        Students students = studentsService.selectById(id);
        List<Ban> bans = banService.selectAllByPlus();
        map.put("bans", bans);
        map.put("student", students);
        return "student/abc :: addOrUpdate";

    }


    @DeleteMapping("delete/{studentId}")
    @ResponseBody
    public Boolean delete(@PathVariable("studentId") Integer id) {
    studentsService.deleteByBanId(id);

          return  true;
    }

  /*  @GetMapping("/query/{studentName}")
    public String selectLike(@RequestParam(required = false ) @PathVariable("studentName")  String studentName, Map<String, Object> map, PageData pageData) {

        Page<Students> page = new Page<>(pageData.getCurrent(), pageData.getSize());
        //模糊查询
        Page<Students> studentsPage = studentsService.selectByLike(studentName, page);
//        分页
//        Page<Students> studentsPage = page.setRecords(students);

        for (Students student : studentsPage.getRecords()) {
            Ban ban = banService.selectOneById(student.getClassId());
            student.setLass(ban);

        }
        if (page.getTotal() != 0) {
            int[] pageNums = MyPageUtil.getNavigatePageNums((int) studentsPage.getCurrent(), (int) studentsPage.getPages(), 3);
            map.put("pageNums", pageNums);
        }

        map.put("page", studentsPage);
        return "student/abc";
    }
*/
}